package com.github.fujianlian.klinechart;

import com.github.fujianlian.klinechart.base.IChartDraw;
import com.github.fujianlian.klinechart.draw.CCIDraw;
import com.github.fujianlian.klinechart.draw.CCIMNDraw;
import com.github.fujianlian.klinechart.draw.KDJDraw;
import com.github.fujianlian.klinechart.draw.MACDDraw;
import com.github.fujianlian.klinechart.draw.MainDraw;
import com.github.fujianlian.klinechart.draw.RSIDraw;
import com.github.fujianlian.klinechart.draw.SKDJDraw;
import com.github.fujianlian.klinechart.draw.VolumeDraw;
import com.github.fujianlian.klinechart.draw.WRDraw;

import java.util.ArrayList;
import java.util.List;

public class ChildDrawType<T extends IChartDraw> {

    public static ChildDrawType mVolumeDraw;
    public static ChildDrawType mMACDDraw;
    public static ChildDrawType mWRDraw;
    public static ChildDrawType mKDJDraw;
    public static ChildDrawType mRSIDraw;
    public static ChildDrawType mMainDraw;
    public static ChildDrawType mCCIDraw;
    public static ChildDrawType mSkdjDraw;
    public static ChildDrawType mCCIMNDraw;
    static int tpCount = 0;

    static {
        mVolumeDraw = new ChildDrawType(VolumeDraw.class, 0, "Volume");
        mMACDDraw = new ChildDrawType(MACDDraw.class, 1, "MACD");
        mWRDraw = new ChildDrawType(WRDraw.class, 2, "WRD");
        mKDJDraw = new ChildDrawType(KDJDraw.class, 3, "KDJ");
        mRSIDraw = new ChildDrawType(RSIDraw.class, 4, "RSI");
        mMainDraw = new ChildDrawType(MainDraw.class, 5, "MAIN_K");
        mCCIDraw = new ChildDrawType(CCIDraw.class, 6, "CCI");
        mSkdjDraw = new ChildDrawType(SKDJDraw.class, 7, "SKDJ");
        mCCIMNDraw = new ChildDrawType(CCIMNDraw.class, 8, "mCCIMNDraw");
        
    }


    int type;
    String msg;
    Class<T> chartDrawClass;

    private final static List<ChildDrawType> typeList = new ArrayList<>();

    public int getType() {
        return type;
    }

    public String getMsg() {
        return msg;
    }

    public static int getTpCount() {
        return tpCount;
    }

    public ChildDrawType(Class<T> chartDrawClass, int type, String msg) {
        ++tpCount;
        this.type = type;
        this.msg = msg;
        this.chartDrawClass = chartDrawClass;
    }

    public static void addChildDrawType(ChildDrawType type) {
        if (type == null || typeList.contains(type)) return;
        if (null != findTypeByT(type.type)) return;
        typeList.add(type);
    }

    public static void removeChildDrawType(ChildDrawType type) {
        if (type == null || !typeList.contains(type)) return;
        typeList.remove(type);
    }


    public static ChildDrawType type(int type) {
        switch (type) {
            case 0:
                return mVolumeDraw;
            case 1:
                return mMACDDraw;
            case 2:
                return mWRDraw;
            case 3:
                return mKDJDraw;
            case 4:
                return mRSIDraw;
            case 5:
                return mMainDraw;
            case 6:
                return mCCIDraw;
            case 7:
                return mSkdjDraw;
            case 8:
                return mCCIMNDraw;
 

        }

        return findTypeByT(type);
    }

    private static ChildDrawType findTypeByT(int type) {
        for (ChildDrawType childDrawType : typeList) {
            if (childDrawType.type == type)
                return childDrawType;
        }

        return mMainDraw;
    }

    public IChartDraw createChildDraw(BaseKLineChartView view) {
        try {
            T t = chartDrawClass.newInstance();
            t.init(view);
            t.setType(type);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
